package com.example.biometrics_demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import java.util.Objects;

import io.flutter.Log;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "com.example/client_channel"; 
    private static final int REQUEST_CODE = 1001;
    private Activity mActivity;
    protected static final int REQUEST_SECURITY_CENTER = 1;
    private MethodChannel.Result result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor().getBinaryMessenger(),  CHANNEL)
            .setMethodCallHandler((call, result) -> {
                // 检查 result 是否为空，如果不为空则reject掉
                if (this.result != null) {
                    this.result.error("1", "未被调用，被新方法调用顶替", "call.method = " + call.method);
                }
                this.result = result;
                if (call.method.equals("recordFace"))  {
                    String faceId = call.argument("faceId");
                    Intent intent = new Intent("com.example.record_face"); 
                    intent.putExtra("face_id", faceId);
                    intent.setPackage("com.example.biometrics"); // 指定目标应用包名 
                    startActivityForResult(intent, REQUEST_CODE);
                } else if (call.method.equals("recognizeFace")) {
                    Intent intent = new Intent("com.example.recognize_face");
                    intent.setPackage("com.example.biometrics");
                    startActivityForResult(intent, REQUEST_CODE);
                } else if (call.method.equals("verifyFace")) {
                    String faceId = call.argument("faceId");
                    Intent intent = new Intent("com.example.verify_face");
                    intent.setPackage("com.example.biometrics"); // 指定目标应用包名
                    intent.putExtra("face_id", faceId); // 使用 putExtra 方法传递 faceId
                    startActivityForResult(intent, REQUEST_CODE);
                }
            });
    }
 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,  resultCode, data);
        Log.d("ActivityResult", "onActivityResult被调用");
        Log.i("ActivityResult", "请求码: " + requestCode + ", 结果码: " + resultCode);

        if (requestCode == REQUEST_CODE && resultCode == 1) {
            String result = data.getStringExtra("result");
            Log.d("ActivityResult", "返回结果值: " + result);
            // 通过通道返回结果到 Flutter 
//            new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor(), CHANNEL)
//                .invokeMethod("onActivityResult", result);
            if (this.result != null) {
                this.result.success(result);
                this.result = null;
            }
        } else {
            Log.d("ActivityResult", "接收失败");
        }
    }
}